package com.ttmusic.view.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import com.ttmusic.activitys.R;
import com.ttmusic.core.MusicManager;
import com.ttmusic.entity.AbstractMusic;
import com.ttmusic.entity.MusicInfo;
import com.ttmusic.view.listeners.OnItemClickListener;


/**
 * 歌手ListView的item被点击后，展示歌曲列表时必须的适配器
 * @author Administrator
 *
 */
public class ArtistMusicAdapter extends MusicAdapter {


    private Context mContext;
    private List<AbstractMusic> list = new ArrayList<AbstractMusic>();
    private OnItemClickListener listener;
    //是否正在多选
    public static boolean isMultiChecking = false;
    
    public ArtistMusicAdapter(Context mContext, List<AbstractMusic> list) {
    	super(mContext, list, false);
    	this.mContext = mContext;
    	this.list = list;
    }
    

    @SuppressWarnings("rawtypes")
	public List getList() {
        return list;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 
     * @param position 当前行号
     * @param convertView 当前行
     * @param parent 父级元素
     */
    @SuppressLint("InflateParams")
	@Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder mViewHolder = null;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_song_music_of_artist, null);
            mViewHolder.song_text = (TextView) convertView.findViewById(R.id.song_title);
            mViewHolder.letter_text = (TextView) convertView.findViewById(R.id.catalog);
            mViewHolder.artist_tv = (TextView) convertView.findViewById(R.id.artist_tv);
            mViewHolder.duration_tv = (TextView) convertView.findViewById(R.id.duration_tv);
            mViewHolder.selected_view = (ImageView)convertView.findViewById(R.id.selected_view);
            mViewHolder.song_item = (LinearLayout) convertView.findViewById(R.id.song_item);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        MusicInfo musicInfo = (MusicInfo) getItem(position);

        mViewHolder.song_text.setText(musicInfo.getTitle());
        mViewHolder.artist_tv.setText(musicInfo.getArtist() + " - " + musicInfo.getAlbum());
        mViewHolder.duration_tv.setText(musicInfo.getDurationStr());
        mViewHolder.letter_text.setVisibility(View.GONE);
        if(isMultiChecking) {
        	mViewHolder.selected_view.setImageResource(R.drawable.icon_checkbox_unchecked);
        	mViewHolder.selected_view.setVisibility(View.VISIBLE);
        }else {
        	mViewHolder.selected_view.setImageResource(R.drawable.playing_icon);
	        if(MusicManager.getPlayingList() == this.list && MusicManager.getNowPlayingIndex() == position) {
	        	mViewHolder.selected_view.setVisibility(View.VISIBLE);
	        }else {
	        	mViewHolder.selected_view.setVisibility(View.GONE);
	        }
        }
//        mViewHolder.song_item.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if(listener!=null){
//                    listener.onItemClick(position);
//                    notifyDataSetChanged();
//                }
//            }
//        });
        return convertView;
    }
    
    /**
     * 开始多选
     */
    public void beginMultiCheck() {
    	isMultiChecking = true;
    	this.notifyDataSetChanged();
    }
    /**
     * 结束多选
     */
    public void cancelMultiCheck() {
    	isMultiChecking = false;
    	this.notifyDataSetChanged();
    }


    final static class ViewHolder {
        TextView song_text;
        TextView letter_text;
        TextView artist_tv;
        TextView duration_tv;
        ImageView selected_view;
        LinearLayout song_item;
    }


	public void setList(List<AbstractMusic> list) {
		this.list = list;
	}


}
